package main

import (
	"fanke/hd"
	"fmt"
	"strings"
)

// 用例
func main() {
	//公钥，开放平台获取
	publicKey := hd.PublicKey
	//接收到的加密参数
	encryptedData := "eYQdSP7mVIeTBMEvYBuckigFKL6RyZeBI2buIedKCPLpaxHFpj5i6Tttd/nn1P2Gz/Hjc94nFJ4p22A9z9pPU3bn6xG3NUiRhP8vIC3BEhcuG5s8voHwN6GhLXQk1AmrdZVDS9JieHxA56FNwTMxovErz7jFCCm6sdx74NIuu5CcoIZ1jNUb4YC98uS8bnuql+AkY+9a1IFpd5xhWroT+bk+vc5rJLZ2IOcYLQYvxZQ2M//Bl8HaN95g6dD7U/VxoKqlwQRSS4+uC79JoRCmcsb1ai1uZ151zPYutqYR6nEv6Jme81e/8nRCgk/i5kC0G/IqYgDtktKRaRwVBqc+5w=="
	//接收到的签名
	sign := "CFCLWZsCZZZ8I5+SeYMtR5mmbXn7feA4gKiPP79RjCXLunANGFASPzcQhIVDbVCz3NwLWFL0YZgKN8cRjSD9PAdiuxs6c+/6YA4ZBr82b4jROrGz/flT9UcJwfRPrBMvmLZh7E56+v6qjJbqJvmF7KiMGIpZjg5MWzx2fDCGTuP0BGzFJmuwHTEFFNiN8ifagFJyZPSL3Q1FVXwJU1wWkDb1vI6jvkC9OqchKK2HckT9cGbjgDnJGYhMzeeoJRUd/nFdzcnHvPut/EEvujrfZad9vCSlcpBUNUfEZYPsCqtxLEkqpvnk25z+reifoNyzq004aiZRHQko88MtD9tq2w=="
	rsaPublicKey := "-----BEGIN CERTIFICATE-----\n" + publicKey + "\n-----END CERTIFICATE-----"
	//奖空格替换成+
	encryptedData = strings.ReplaceAll(encryptedData, " ", "+")

	sign = strings.ReplaceAll(sign, " ", "+")
	//获取到公钥
	rsaPubKey, err := hd.ParsePublicKey(rsaPublicKey)
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
	//进行验签
	pass, err := hd.RsaVerifySignWithMd5(encryptedData, sign, publicKey)
	if err != nil {
		fmt.Println("验签不通过,err", err)
	}
	fmt.Println("验签结果：", pass)
	//使用公钥进行解密
	decrypt, err := hd.PublicDecrypt(rsaPubKey, encryptedData)
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
	fmt.Println("解密数据：" + decrypt)
	//获取B端请求数据的accessToken
	token, err := hd.GetAccessToken()
	if err != nil {
		fmt.Println("GetAccessToken,err", err)
	} else {
		fmt.Println("获取到的AccessToken：" + token.AccessToken)
	}

	//获取某个活动的信息
	info, err := hd.GetActivitiesInfo(28630301, 1)
	if err != nil {
		fmt.Println("GetActivitiesInfo err")
	}
	fmt.Println("活动信息：", info)

	//C端用户授权获取code（旧） 不推荐使用此方法获取
	code, err := hd.GetCode("", "testUid", "", "", "")
	if err != nil {
		fmt.Println("getCode rer")
	}
	//C端用户使用code换取partnerAccessToken（旧） 不推荐使用此方法获取
	partnerAccessToken, err := hd.GetPartnerAccessToken(code)
	if err != nil {
		fmt.Println("getToken err")
	}
	fmt.Println("获取到的PartnerAccessToken:" + partnerAccessToken.AccessToken)

	//服务端直接方式获取partnerAccessToken 推荐使用
	partnerAccessTokenDirect, err := hd.GetPartnerAccessTokenDirect("testUid2", "", "", "")
	if err != nil {
		fmt.Println("getToken err")
	}
	fmt.Println("服务端直接方式获取到的PartnerAccessToken:" + partnerAccessTokenDirect.AccessToken)

	//获取到某个uid的C端用户参与活动的链接
	url, err := hd.GetJointActivityUrl(28630301, 1, "testUid3")
	if err != nil {
		fmt.Println("GetJointActivityUrl err")
	}
	fmt.Println("testUid3拼接提供给用户访问的活动链接:", url)

}
